package com.educationsystem.controller;

import java.io.IOException;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebFilter(dispatcherTypes = {DispatcherType.REQUEST },
filterName="Filterlogin", urlPatterns={"*.jsp"})
public class CharacterEncodingFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		//response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+"/";
		String url = req.getRequestURL().toString();
		//System.out.println(url);
		String sf = (String) req.getSession().getAttribute("sf"); 
		if(url.equals(basePath+"error.jsp")||url.equals(basePath+"page-login.jsp")) {
			chain.doFilter(request, response);
		}else if (sf != null) 
		{
			if(url.equals(basePath+"nav-admin.jsp")
					||url.equals(basePath+"nav-student.jsp")
					||url.equals(basePath+"nav-teacher.jsp")
					||url.equals(basePath+"top.jsp")) {
				res.sendRedirect("error.jsp");
			}
			else if(sf.equals("student")||sf.equals("teacher")) 
			{
				if(url.equals(basePath+"addAdmin.jsp")
						||url.equals(basePath+"addFile.jsp")
						||url.equals(basePath+"addNotice.jsp")
						||url.equals(basePath+"addStudent.jsp")
						||url.equals(basePath+"addTeacher.jsp")
						||url.equals(basePath+"updateAdmin.jsp")
						||url.equals(basePath+"updateFile.jsp")
						||url.equals(basePath+"updateNotice.jsp")
						||url.equals(basePath+"AdminTables.jsp")) {
					res.sendRedirect("error.jsp");
				}else if(sf.equals("student")) {
					if(url.equals(basePath+"addTopice.jsp")
							||url.equals(basePath+"updateTopice.jsp")
							||url.equals(basePath+"updateTeacher.jsp")
							||url.equals(basePath+"updateSelected.jsp")) {
						res.sendRedirect("error.jsp");
					}else chain.doFilter(request, response);
				}else if(sf.equals("teacher")) {
					if(url.equals(basePath+"addSelected.jsp")
							||url.equals(basePath+"updateStudent.jsp")) {
						res.sendRedirect("error.jsp");
					}else chain.doFilter(request, response);
				}
			}else if(sf.equals("admin")) {
				chain.doFilter(request, response);
			}else {
				res.sendRedirect("error.jsp");
			}
		}else {
			res.sendRedirect("page-login.jsp");
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
